//
//  ChanceController.m
//  谈诗说词
//
//  Created by dllo on 15/6/13.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ChanceController.h"
#import "DB.h"
#import "PoemModel.h"
#import "PoemDatabase.h"
#import "SearchListController.h"
#import "Database.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ChanceController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ChanceController
- (void)dealloc
{
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        self.NameArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0.32 * WIDTH, 0.08 * WIDTH)];
    title.text = @"筛选";
    title.font = [UIFont fontWithName:@"-" size:28];
    title.textAlignment = 1;
    self.navigationItem.titleView = title;
    NSMutableArray *bigArray = [NSMutableArray arrayWithArray:[PoemDatabase selectAll]];
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < bigArray.count; i++) {
        PoemModel *model = [bigArray objectAtIndex:i];
        [arr addObject:model.poet_name];
        [PoemDatabase insertWithString:model.poet_name];
    }
    
    
    self.NameArray = [NSMutableArray arrayWithArray:[Database selectAll]];
    [self createView];
    [self createButton];
}

- (void)createView
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [imageView setImage:[UIImage imageNamed:@"筛选背景.png"]];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    
    UITableView *table1 = [[UITableView alloc]initWithFrame:CGRectMake(0.05 * WIDTH, 0.6 * WIDTH, 0.9 * WIDTH, 0.6 * WIDTH) style:UITableViewStylePlain];
    table1.rowHeight = 40;
    table1.delegate = self;
    table1.dataSource = self;
    [table1 setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:table1];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.NameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [self.NameArray objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont fontWithName:@"-" size:23];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


- (void)createButton
{
    UIButton *suitang = [[UIButton alloc]initWithFrame:CGRectMake(0.04 * WIDTH, 0.23 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
        [suitang setTitle:@"隋唐" forState:UIControlStateNormal];
    suitang.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [suitang setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [suitang addTarget:self action:@selector(stAction) forControlEvents:UIControlEventTouchUpInside];
    //suitang.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:suitang];
    
    
    UIButton *xianqin = [[UIButton alloc]initWithFrame:CGRectMake(0.3 * WIDTH, 0.23 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
    [xianqin setTitle:@"先秦" forState:UIControlStateNormal];
    xianqin.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [xianqin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [xianqin addTarget:self action:@selector(xqAction) forControlEvents:UIControlEventTouchUpInside];
    //xianqin.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:xianqin];
    
    UIButton *handai = [[UIButton alloc]initWithFrame:CGRectMake(0.56 * WIDTH, 0.23 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
    [handai setTitle:@"汉代" forState:UIControlStateNormal];
    handai.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [handai setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [handai addTarget:self action:@selector(hdAction) forControlEvents:UIControlEventTouchUpInside];
    //handai.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:handai];
    
    UIButton *wejin = [[UIButton alloc]initWithFrame:CGRectMake(0.04 * WIDTH, 0.38 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
    [wejin setTitle:@"魏晋" forState:UIControlStateNormal];
    wejin.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [wejin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [wejin addTarget:self action:@selector(wjAction) forControlEvents:UIControlEventTouchUpInside];
    //wejin.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:wejin];
    
    
    UIButton *songdai = [[UIButton alloc]initWithFrame:CGRectMake(0.3 * WIDTH, 0.38 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
    [songdai setTitle:@"宋代" forState:UIControlStateNormal];
    songdai.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [songdai setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [songdai addTarget:self action:@selector(sdAction) forControlEvents:UIControlEventTouchUpInside];
    //songdai.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:songdai];
    
    
    UIButton *xiandai = [[UIButton alloc]initWithFrame:CGRectMake(0.56 * WIDTH, 0.38 * WIDTH, 0.21 * WIDTH, 0.1 * WIDTH)];
    [xiandai setTitle:@"现代" forState:UIControlStateNormal];
    xiandai.titleLabel.font = [UIFont fontWithName:@"-" size:23];
    [xiandai setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [xiandai addTarget:self action:@selector(xdAction) forControlEvents:UIControlEventTouchUpInside];
    //songdai.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:xiandai];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.NameArray objectAtIndex:indexPath.row];
    SearchListController *list = [[SearchListController alloc]init];
    list.key = str;
    [self.navigationController pushViewController:list animated:YES];
}

- (void)stAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"隋唐";
    [self.navigationController pushViewController: list animated:YES];
    
}
- (void)xqAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"先秦";
    [self.navigationController pushViewController: list animated:YES];
}
- (void)hdAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"汉代";
    [self.navigationController pushViewController: list animated:YES];
}
- (void)wjAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"魏晋";
    [self.navigationController pushViewController: list animated:YES];
}
- (void)sdAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"宋代";
    [self.navigationController pushViewController: list animated:YES];
}
- (void)xdAction
{
    SearchListController *list = [[SearchListController alloc]init];
    list.key = @"现代";
    [self.navigationController pushViewController: list animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
